package com.guli.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.guli.common.exception.NoStockException;
import com.guli.ware.vo.LockStockResult;
import com.guli.ware.vo.WareSkuLockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.guli.ware.entity.WareSkuEntity;
import com.guli.ware.service.WareSkuService;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;


/**
 * 商品库存
 * @author zzb
 * @email 1626131069@qq.com
 * @date 2023-11-24 22:39:12
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;

    /**
     * 根据订单id解锁库存
     * @param wareSkuLockVo
     * @return
     */

    @PostMapping("/lock/order")
    public R orderLockStock(@RequestBody WareSkuLockVo wareSkuLockVo) {
        try {
            Boolean isLocked = wareSkuService.orderLockStock(wareSkuLockVo);
            return R.ok();
        } catch (NoStockException ex){
            return R.error(21000,"商品库存不足");
        }
    }


    /**
     * 查询库存和重量
     * @param skuIds
     * @return
     */
    @GetMapping("/hasStockAndWeight")
    public R hasStocks(@RequestParam List<Long> skuIds) {
        return wareSkuService.hasStocks(skuIds);
    }


    /**
     * 查询该商品是否有库存
     * @param skuId
     * @return
     */
    @GetMapping("/hasStock/{skuId}")
    public R hasStock(@PathVariable Long skuId) {
        R stock = wareSkuService.hasStock(skuId);
        return stock;
    }

    /**
     * 展示库存列表
     * @param params
     * @return
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = wareSkuService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        WareSkuEntity wareSku = wareSkuService.getById(id);
        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody WareSkuEntity wareSku) {
        wareSkuService.save(wareSku);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody WareSkuEntity wareSku) {
        wareSkuService.updateById(wareSku);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        wareSkuService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
